<?php
namespace app\common\model;

class LiveView extends Common
{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'ctime';
    /**
     * 进入直播间
     */
    public function onView($data){
        $view=$this->where(['room_id'=>$data['room_id'],'live_id'=>$data['live_id'],'uid'=>$data['uid']])->find();
        if($view){
            $view->nums=['inc',1];
            $view->utime=time();
            $res=$view->save();
        }else{
            $data['nums']=1;
            $res=$this->create($data);
            //更新直播间
            Room::where('id',$data['room_id'])->setInc('views',1);
            Live::where('id',$data['live_id'])->setInc('views',1);
        }
        if(!$res){
            return [false,'操作失败'];
        }
        return [true,'success'];
    }
    /**
     * 退出直播间
     */
    public function offView($data){
        $view=$this->where(['room_id'=>$data['room_id'],'live_id'=>$data['live_id'],'uid'=>$data['uid']])->find();
        if($view){
            $view->is_line=1;
            $view->utime=time();
            $view->save();
            return [true,'success'];
        }
        return [false,'操作失败'];
    }
}
